﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Shop.CartResources;
using Shop.Data;
using Shop.Resources;
using Telerik.WinControls.UI;

namespace Shop.AllForms
{
    public partial class BuyProducts : Form
    {
        public CartResources.Cart CurrentCart { get; set; }

        public BuyProducts()
        {
            InitializeComponent();
            CurrentCart = new CartResources.Cart();
            this.radGridViewCart.AutoGenerateColumns = true;
            this.radGridViewCart.DataSource = CurrentCart.CartItems;
        }

        private void radTextBoxSearchProducts_TextChanged(object sender, EventArgs e)
        {
            string inputText = this.radTextBoxSearchProducts.Text;
            if (inputText.Length > 0)
            {
                try
                {
                    AutoCompleteStringCollection availableProducts = Data.AutoCompleteData.GetAvailableProducts(inputText);
                    this.radTextBoxSearchProducts.AutoCompleteCustomSource = availableProducts;
                }
                catch (AccessViolationException)
                {

                }
            }
        }

        private void radTextBoxSearchProducts_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.radButtonAddProduct_Click(this, e);
            }
        }

        private void radButtonAddProduct_Click(object sender, EventArgs e)
        {
            string productString = this.radTextBoxSearchProducts.Text;
            if (productString != string.Empty)
            {
                try
                {
                    string barcode = GetBarcodeFromAutocomplete(productString);
                    IEnumerable<ICartItem> itemsWithSameBarcode = this.CurrentCart.CartItems.Where(currentItem => currentItem.Barcode == barcode);
                    CartResources.CreateCartItemForm createCartItemForm = new CartResources.CreateCartItemForm(barcode, itemsWithSameBarcode);
                    createCartItemForm.ShowDialog();
                    if (createCartItemForm.DialogResult == System.Windows.Forms.DialogResult.OK)
                    {
                        this.CurrentCart.AddProduct(createCartItemForm.ConvertedCart);
                    }
                }
                catch (ArgumentException)
                {
                    MessageBox.Show("Грешен продукт");
                }
            }
            this.radLabelFullPrice.Text = CurrentCart.FullCartPrice.ToString();
        }
  
        private string GetBarcodeFromAutocomplete(string text)
        {
            string[] textElements = text.Split(' ');
            if (textElements[0].ContainsOnlyNumbers())
            {
                return textElements[0];
            }
            else if (textElements[textElements.Length - 1].Trim('"').ContainsOnlyNumbers())
            {
                return textElements[textElements.Length - 1].Trim('"');
            }
            else
            {
                throw new ArgumentException("Invalid autocomplete string");
            }
        }

        private void radButtonRemoveProduct_Click(object sender, EventArgs e)
        {
            GridViewRowInfo[] selectedRows = this.radGridViewCart.SelectedRows.ToArray();
            foreach (GridViewRowInfo currentItemToDelete in selectedRows)
            {
                currentItemToDelete.Delete();
            }
        }

        private void radButtonCashDocument_Click(object sender, EventArgs e)
        {
            Shop.CartResources.Payment paymentForm = new Payment(this.CurrentCart);
            paymentForm.ShowDialog();
            if (paymentForm.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                GridViewRowInfo[] rows = this.radGridViewCart.Rows.ToArray();
                foreach (GridViewRowInfo currentItemToDelete in rows)
                {
                    currentItemToDelete.Delete();
                }
            }
        }
    }
}
